<template>
  <!-- 商品列表组件 -->
  <div class="myList">
    <ul>
      <li v-for="item in listData" :key="item.product_id">
        <el-popover placement="top">
          <p>确定删除吗？</p>
          <div style="text-align: right; margin: 10px 0 0">
            <el-button type="primary" size="mini" @click="deleteCollect(item.product_id)"
              >确定</el-button
            >
          </div>
          <i class="el-icon-close delete" slot="reference" v-show="isDelete"></i>
        </el-popover>
        <router-link :to="{ path: '/details', query: { productID: item.product_id } }">
          <img :src="$target + item.product_picture" alt="" />
          <h2>{{ item.product_name }}</h2>
          <h3>{{ item.product_title }}</h3>
          <p>
            <span>{{ item.product_selling_price }}元</span>
            <del v-if="item.product_selling_price != item.product_price"
              >{{ item.product_price }}元</del
            >
          </p>
        </router-link>
      </li>
      <li id="more" v-show="!moreShow">
        <router-link
          :to="{ path: 'allProducts', query: { categoryID: this.arrayId, switch: true } }"
          class="to"
        >
          <span>浏览更多</span>
          <i class="el-icon-d-arrow-right"></i>
        </router-link>
      </li>
    </ul>
  </div>
</template>
<script>
import axios from "../api/detailsApi";
export default {
  name: "MyList",
  props: ["listData", "moreShow", "isDelete"],
  data() {
    return {};
  },
  methods: {
    // 删除收藏 
    async deleteCollect(id) {
      let obj = {
        user_id: this.$store.state.userId,
        product_id: id,
      };
      let { data: res } = await axios.deleteCollect(obj);
      switch (res.code) {
        case "001":
          for (let i = 0; i < this.listData.length; i++) {
            let temp = this.listData[i];
            if (temp.product_id == id) {
              this.listData.splice(i, 1);
            }
          }
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
          });
          break;

        default:
          this.$notify.error({
            title: "失败",
            message: res.msg,
          });
      }
    },
  },
  computed: {
    arrayId() {
      let arr = [];
      this.listData.forEach((item) => {
        if (!arr.includes(item.category_id)) {
          arr.push(item.category_id);
        }
      });
      return arr;
    },
  },
};
</script>
<style lang="scss" scoped>
.myList {
  li {
    position: relative;
    float: left;
    padding: 10px 0;
    margin: 0 0 14.5px 13.7px;
    width: 234px;
    height: 300px;
    background-color: #fff;
    transition: all 0.2s linear;
    z-index: 1;
    &:hover {
      z-index: 2;
      -webkit-box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
      box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
      -webkit-transform: translate3d(0, -2px, 0);
      transform: translate3d(0, -2px, 0);
    }
    &#more {
      text-align: center;
      line-height: 280px;
      font-size: 18px;
      a {
        color: #333;
      }
      a:hover {
        color: #ff6700;
      }
    }

    img {
      display: block;
      width: 160px;
      height: 160px;
      background: url("../assets/imgs/placeholder.png") no-repeat 50%;
      margin: 0 auto;
    }
    h2,
    h3 {
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
    h2 {
      margin: 25px 10px 0;
      font-size: 14px;
      color: #333;
      font-weight: 400;
      text-align: center;
    }
    h3 {
      margin: 5px 10px;
      height: 18px;
      font-size: 12px;
      font-weight: 400;
      color: #b0b0b0;
      text-align: center;
    }
    p {
      margin: 10px 10px 10px;
      text-align: center;
      color: #ff6700;
      del {
        margin-left: 0.5em;
        color: #b0b0b0;
        text-decoration: line-through;
      }
    }
  }
}
.myList ul li .delete {
  position: absolute;
  top: 10px;
  right: 10px;
  display: none;
}
.myList ul li:hover .delete {
  display: block;
}
.myList ul li .delete:hover {
  color: #ff6700;
}
</style>
